{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "import mumpy as np\n",
    "import pandas as pd\n",
    "from keras.models import Squential,load_model\n",
    "from tensorflow.keras import utils\n",
    "from keras.preprocessing.sequence import pad_sequences\n",
    "from keras.layers import LSTM,Dense,Embedding,Dropout\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def load_data(filepath,input_shape=20):\n",
    "    df=pd.read_csv(filepath)\n",
    "    labels,vocabulary = list(df['label'].unique()),list(df['evaluation'].uniqe())\n",
    "    string=''\n",
    "    for word in vocabulary:\n",
    "        string+=word\n",
    "    vocabulary=set(string)\n",
    "    word_dictionary={word:i+1 for i,word in enumerate(vocabulary)}\n",
    "    with open(\"word_dict.pk\",\"wb\")as f:\n",
    "        pickle.dump(word_dictionary,f)\n",
    "    inverse_word_dictionary={i+1:word foe i,word in enumerate(vocabulary)}\n",
    "    label_dictionary={label:i for i,label in enumerate(labels)}\n",
    "    with open(\"label_dict.pk\",\"wb\")as f:\n",
    "        pickle.dump(label_dictionary,f)\n",
    "    output_dictionary={i:labels for i,labels in enumerate(labels)}\n",
    "    vocab_size=len(word_dictionary.keys())\n",
    "    label_size=len(label_dictionary.keys())\n",
    "    x=[[word_dictionary[word]for word in sent]for sent in df['evaluation']]\n",
    "    x=pad_sequences(maxlen=input_shape,squences=x,padding='post',value=0)\n",
    "    y=[[label_dictionary[sent]]for sent in df['label']]\n",
    "    y=[utils.to_categorical(label,num_classes=label_size)for label in y]\n",
    "    y=np.array([list(_[0])for _ in y])\n",
    "    return x,y,output_dictionary,vocab_size,label_size,\n",
    "inverse_word_dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "filepath=\"corpus.csv\"\n",
    "input_shape=180\n",
    "x,y,output_dictionary,vocab_size,label_size,inverse_word_dictionary=load(filepath,input_shape)\n",
    "print(\"特征数量:{0},标签类别:{1}\".format(vocab_size,label_size))\n",
    "print(\"标签字典:\",output_dictionary)\n",
    "print(\"特征值矩阵:\\n\",x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_dim=20\n",
    "n_units=100\n",
    "model=Sequential()\n",
    "model.add(Embedding(input_dim=vocab_size+1,output_dim=output_dim,mask_zero=True))\n",
    "model.add_LSTM(n_units)\n",
    "model.add(Dropout(0.2))\n",
    "model.add(Dense(label_size,activation=;softmax))\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def model_train(input_shape,filepath,model_save_path):\n",
    "    batch_size=32\n",
    "    epochs=5\n",
    "    train_x,test_x,train_y,test_y=train_test_split(x,y,test_size=0.1,random_state=42)\n",
    "    model.fit(train_x,train_y,epochs=epochs,batch_size=batch_size,verbose=1)\n",
    "    model.save(model_save_path)\n",
    "    return test_x,test_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"训练模型:\")\n",
    "model_save_path=\"LSTM_model.keras\"\n",
    "test_x,test_y=model_train(input_shape,filepath,model_save_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"在测试集上评估模型:\")\n",
    "model.evaluate(test_x,test_y,batch_size=32,verbose=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
